package com.shinewind.info.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.info.mapper.ICommentActionMapper;
import com.shinewind.info.service.ICommentActionService;
import com.shinewind.pojo.entity.CommentAction;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentActionServiceImpl extends ServiceImpl<ICommentActionMapper, CommentAction> implements ICommentActionService {
    @Override
    public Long getSumLoveBycId(String cid) {
        Long sum = this.lambdaQuery().eq(CommentAction::getCId,cid).eq(CommentAction::getLove,1).count();
        return sum;
    }

    @Override
    public List<String> getLoveListByUid(String uId) {
        //查询用户已经点赞的评论列表，返回List<String>
        if(this.lambdaQuery().eq(CommentAction::getLove,1).eq(CommentAction::getUId,uId).count() > 0){
            List<CommentAction> commentActions = this.lambdaQuery().eq(CommentAction::getLove,1)
                    .eq(CommentAction::getUId,uId).list();
            List<String> loveList = commentActions.stream().map(commentAction -> {
                return commentAction.getCId();
            }).collect(Collectors.toList());
            return loveList;
        }
        else{
            List<String> loveList = new ArrayList<String>();
            loveList.add("你还没有任何点赞");
            return loveList;
        }
    }

    @Override
    public Boolean cancelLoveByUidANDcId(String cId, String uId) {
        //取消点赞方法
        return this.lambdaUpdate().eq(CommentAction::getCId,cId).eq(CommentAction::getUId,uId)
                .set(CommentAction::getLove,0).update();
    }

    @Override
    public Boolean addLoveByUidANDcId(String cId, String uId) {
        //添加点赞方法
        //如果数据库中有记录且love为0，直接修改love的值
        if(this.lambdaQuery().eq(CommentAction::getCId,cId).eq(CommentAction::getUId,uId).count() > 0){
            return this.lambdaUpdate().eq(CommentAction::getCId,cId).eq(CommentAction::getUId,uId)
                    .set(CommentAction::getLove,1).update();
        }
        //如果数据库中没有该条点赞记录，添加记录
        else{
            CommentAction commentAction = CommentAction.builder().love(1l).cId(cId).uId(uId).build();
            return this.save(commentAction);
        }
    }
}
